<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Cart;
use App\Models\Goods;
use App\Models\SpecGoodsPrice;
use DB,Log;
class CartController extends Controller
{
    
    /*
	 * 购物车列表
	 * */
    protected function cartList(Request $request){
		$uid = $request->uid;
    	$cart = Cart::where('uid',$uid)->select('cart_id','uid','goods_id','goods_name','img','goods_price','goods_num','spec_key','selected','spec_key_name')
    		->get();

		return response()->json([
			'code'	=> '00000',
			'msg'	=> 'ok',
			'data'	=> array(
				'cartlist'	=> $cart,
				'total'		=> $this->getTotal($uid)
				)
			]);			
    }
	
	/*
	 * 购物车已选择商品
	 * 
	 * 
	 * */
	protected function cartSelected(Request $request){
		$uid =$request->uid;
    	$cart = Cart::where('selected',1)
    		->where('uid',$uid)->select('cart_id','uid','goods_id','goods_name','img','goods_price','goods_num','spec_key','selected','spec_key_name')->get();
		$freight = 0;
		$goods_price = $this->getTotal($uid);
		return response()->json([
			'code'	=> '00000',
			'msg'	=> 'ok',
			'data'	=> array(
				'cartlist'	=> $cart,
				'freight'		=>	$freight,
				'goods_price'	=> $goods_price,
				'total_amount'	=> sprintf("%.2f", $goods_price+$freight),
				'order_amount'	=> sprintf("%.2f", $goods_price+$freight)
				)
				
			]);			
	}
	
	/*
	 * 立即付款--获取立即付款商品
	 * 
	 * */
	 protected function goodspay(Request $request){
	 	$goods_id = $request->input('goods_id');
		$goods = Goods::select('goods_id','goods_name','original_img','shop_price')->find($goods_id);
		if($request->has('goods_num')){
			$goods->goods_num = $request->input('goods_num');
		}else{
			$goods->goods_num = 1;
		}
		if($request->has('spec_key')){
			$spec_key = $request->input('spec_key');
			$res_key_name = SpecGoodsPrice::where('key',$spec_key)->first()['key_name'];
			if($res_key_name){
				$goods->key_name = $res_key_name;
				$goods->spec_key = $spec_key;					
			}else{
				$goods->key_name = '';
				$goods->spec_key = '';						
			}
					
		}else{
			$goods->key_name = '';
			$goods->spec_key = '';			
		}

		$freight = 0;
		$goods_price = sprintf("%.2f", $goods->shop_price*$goods->goods_num);
		return response()->json([
			'code'	=> '00000',
			'msg'	=> 'ok',
			'data'	=> array(
				'goods'	=> $goods,
				'freight'		=>	$freight,
				'goods_price'	=> $goods_price,
				'total_amount'	=> sprintf("%.2f", $goods_price+$freight),
				'order_amount'	=> sprintf("%.2f", $goods_price+$freight)
				)
				
			]);			 	
	 }
	
	
	/*
	 * 加入购车
	 * 
	 * */
	protected function add(Request $request){
		$goods_id = $request->input('goods_id');
		$goods_num = $request->input('goods_num');
		$uid = $request->uid;
		$goods = Goods::find($goods_id);

			
		if($request->input('spec_ids')){
			$spec_ids = $request->input('spec_ids');
			if($cart = Cart::where('uid',$uid)->where('goods_id',$goods_id)->where('spec_key',$spec_ids)->first()){
				$cart->goods_num	= $cart->goods_num + $goods_num;
				if($cart->save()){
					return response()->json([
						'code'	=> '00000',
						'msg'	=> '加入成功',
						'data'	=> ''
						]);				
				}else{
					return response()->json([
						'code'	=> '10001',
						'msg'	=> '加入购车失败',
						'data'	=> ''
						]);				
				}					
			}
		}else{
			if($cart = Cart::where('uid',$uid)->where('goods_id',$goods_id)->first()){
				$cart->goods_num = $cart->goods_num + $goods_num;
				if($cart->save()){
					return response()->json([
						'code'	=> '00000',
						'msg'	=> '加入成功',
						'data'	=> ''
						]);				
				}else{
					return response()->json([
						'code'	=> '10001',
						'msg'	=> '加入购车失败',
						'data'	=> ''
						]);				
				}				
			}
		}
		
		$cart = new Cart;
		$cart->uid		= $uid;
		$cart->goods_id		= $goods->goods_id;
		$cart->goods_name	= $goods->goods_name;
		$cart->goods_num	= $goods_num;
		$cart->img			= $goods->original_img;
		$cart->cost_price	= $goods->cost_price;
		if($request->input('spec_ids')){
//			$spec_ids = $request->input('spec_ids');
			if($specGoodsPrice = SpecGoodsPrice::where('key',$spec_ids)->first()){
				$cart->spec_key = $spec_ids;
				$cart->spec_key_name = $specGoodsPrice->key_name;
				$cart->goods_price	= $specGoodsPrice->price;			
			}
		}else{
			$cart->goods_price	= $goods->shop_price;
		}
		if($cart->save()){
			return response()->json([
				'code'	=> '00000',
				'msg'	=> '加入成功',
				'data'	=> ''
				]);				
		}else{
			return response()->json([
				'code'	=> '10001',
				'msg'	=> '加入购车失败',
				'data'	=> ''
				]);				
		}
	}
	
	/*
	 * 删除
	 * 
	 * */
	protected function cartDel(Request $request){
		$cart_ids = $request->input('cart_ids');
		$cart_ids = json_decode($cart_ids);

		if(Cart::whereIn('cart_id',$cart_ids)->delete()){
			return response()->json([
				'code'	=> '00000',
				'msg'	=> '删除成功',
				'data'	=> ''
				]);					
		}else{
			return response()->json([
				'code'	=> '10001',
				'msg'	=> '删除失败',
				'data'	=> ''
				]);					
		}
	}
	
	/*
	 * 设置数量
	 * 
	 * */
	protected function setCartNum(Request $request){
		$cart_id = $request->input('cart_id');
		$goods_num = $request->input('goods_num');
		$uid = $request->uid;
		$cart = Cart::find($cart_id);
		$num = $cart->goods_num + $goods_num;
		if($num >= 1){
			$cart->goods_num = $num;
		}else{
			$cart->goods_num = 1;
		}
		if($cart->save()){
			return response()->json([
				'code'	=> '00000',
				'msg'	=> '设置成功',
				'data'	=> array(
					'total'	=> $this->getTotal($uid)
					)
				
				]);					
		}else{
			return response()->json([
				'code'	=> '10001',
				'msg'	=> '设置失败',
				'data'	=> ''
				]);					
		}		
	}
	
	/*
	 * 设置选中状态
	 * 
	 * */
	protected function setSelected(Request $request){
		$cart_id = $request->input('cart_id');
		$selected = $request->input('selected');
		$uid = $request->uid;
		if($selected==="all"){
			if(Cart::where('uid',$uid)->update(['selected'=>1])){
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '设置成功',
					'data'	=> array(
						'total'	=> $this->getTotal($uid)
						)
					]);					
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '设置失败',
					'data'	=> ''
					]);					
			}			
		}elseif($selected==="cancel"){
			if(Cart::where('uid',$uid)->update(['selected'=>0])){
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '设置成功',
					'data'	=> array(
						'total'	=> $this->getTotal($uid)
						)
					]);					
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '设置失败',
					'data'	=> ''
					]);					
			}			
		}else{
			$cart = Cart::find($cart_id);
			$cart->selected = $selected;
			if($cart->save()){
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '设置成功',
					'data'	=> array(
						'total'	=> $this->getTotal($uid)
						)
					]);					
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '设置失败',
					'data'	=> ''
					]);					
			}				
		}

		
	}
	
	
	/*
	 * 获取购物车总价
	 * 
	 * */
	private function getTotal($uid){
		$total = Cart::where('uid',$uid)->where('selected',1)->select(DB::raw('sum(goods_price*goods_num) as total'))->first()['total'];
		if($total){
			return $total;
		}else{
			return '0.00';
		}
	}
	
}
